package com.luocj.rxdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Func0;
import rx.functions.Func2;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName() + "---------------------->";
    private String values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        hello world
        findViewById(R.id.btn1).setOnClickListener(this);
//        操作符 creat
        findViewById(R.id.btn2).setOnClickListener(this);
//        操作符 from
        findViewById(R.id.btn3).setOnClickListener(this);
//        操作符 dfer
        findViewById(R.id.btn4).setOnClickListener(this);

   /*
   * 组合类操作符
   *
   * */

        findViewById(R.id.btn31).setOnClickListener(this);
        findViewById(R.id.btn32).setOnClickListener(this);
        findViewById(R.id.btn33).setOnClickListener(this);
        findViewById(R.id.btn34).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn34:
                //
                testCombineLatest();
                break;
            case R.id.btn33:
                //在某一个被观察者之前
                testStartWith();
                break;
            case R.id.btn32:
                //根据时间戳合并2个被观察者
                testMerge();
                break;
            case R.id.btn31:
                testZip();
                break;
            case R.id.btn1:
                //创建一个
                helloworld();
                Log.i(TAG, "onClick: " + (152.4 - 141.8));
                break;

            case R.id.btn2:
                //creat just
//                Observable.create(new Observable.OnSubscribe<String>() {
//                    @Override
//                    public void call(Subscriber<? super String> subscriber) {
//                        subscriber.onNext("haha--->");
//                        subscriber.onCompleted();
//                    }
//                }).subscribe(new Subscriber<String>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        Log.i(TAG, "onNext: "+ s);
//                    }
//                });
                Observable.just("haha").subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.i(TAG, "onNext: " + s);
                    }
                });
                break;

            case R.id.btn3:
                ArrayList<String> strings = new ArrayList<>();
                strings.add("1");
                strings.add("2");
                strings.add("3");
                strings.add("4");
                strings.add("5");
                strings.add("6");
                Observable.from(strings).subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.i(TAG, "onNext: " + s);
                    }
                });
                break;
            case R.id.btn4:
                //defer
                values = "调用subscribe 之前赋值就行";
                Observable<String> defer = Observable.defer(new Func0<Observable<String>>() {
                    @Override
                    public Observable<String> call() {
                        return Observable.just(values);
                    }
                });
                defer.subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted: ");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.i(TAG, "onNext: " + s);
                    }
                });
                break;

        }
    }

    private void testCombineLatest() {
        Observable<Integer> just = Observable.just(1, 3, 4);
        Observable<Integer> just1 = Observable.just(4, 5, 6, 7);
        just.combineLatest(just, just1, new Func2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                Log.i(TAG, "call: " + "integer" + integer + "integer2" + integer2);
                return integer + integer2;
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext: " + integer);
            }
        });
    }

    private void testStartWith() {
        Observable<Integer> just = Observable.just(1, 3, 4);
        Observable<Integer> just2 = Observable.just(3, 3, 4);
        just2.startWith(just).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext: " + integer);
            }
        });
    }

    private void testMerge() {
        Observable<Integer> just = Observable.just(11, 22, 33);
        Observable<Integer> just1 = Observable.just(33, 44, 55);
        Observable.merge(just, just1).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext: " + integer);
            }
        });
    }

    private void testZip() {
        Observable<Integer> observable = Observable.just(2, 3, 4);
        Observable<Integer> observable1 = Observable.just(3, 4, 5, 6);
        Observable.zip(observable, observable1, new Func2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG, "onNext: " + integer);
            }
        });
    }


    private void helloworld() {
        //被观察者
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello world");
                subscriber.onCompleted();
            }
        });

        //观察者
        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.i(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: " + e.toString());
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: " + s);
            }
        };

        observable.subscribe(subscriber);

    }
}
